<?php

/**
 * This is the model class for table "zy_package".
 *
 * The followings are the available columns in table 'zy_package':
 * @property integer $package_id
 * @property string $package_name
 * @property string $package_content
 * @property integer $package_price
 * @property integer $package_type
 * @property integer $service_id
 * @property integer $channel_id
 */
class Package extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Package the static model class
	 */
	 
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_package';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('package_name, package_price, package_type, service_id', 'required'),
			array('package_type, service_id', 'numerical', 'integerOnly'=>true),
			array('package_name, package_content', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('package_id, package_name, package_content, package_price, package_type, service_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'package_id' => Yii::t('base','Package'),
			'package_name' => Yii::t('base','Package Name'),
			'package_content' => Yii::t('base','Package Content'),
			'package_price' => Yii::t('base','Package Price'),
			'package_type' => Yii::t('base','Package Type'),
			'service_id' => Yii::t('base','Service Name'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$flag = Yii::app()->request->getParam('flag');
		$criteria=new CDbCriteria;
		$criteria->compare('package_id',$this->package_id);
		$criteria->compare('package_name',$this->package_name,true);
		$criteria->compare('package_content',$this->package_content,true);
		$criteria->compare('package_price',$this->package_price);
		$criteria->compare('package_type',$this->package_type);
		if($flag == 'TV'){
			$this->service_id = 14;
			$criteria->compare('service_id',$this->service_id);
		}else{
			$criteria->condition = 'service_id <> 14';
		}
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(
				'defaultOrder'=>'service_id DESC',
			),
		));
	}
	//获取Service Name
	public function getServiceName()
	{
		$rs = Service::model()->findAll();
		return CHtml::listData($rs,'service_id','service_name');
	}
	//admin中显示service name
	public function showServiceName($id)
	{
		$rs = Package::model()->findByPk($id);
		$rs2 = Service::model()->find('service_id='.$rs->service_id);
		return $rs2->service_name;
	}
	// admin中显示PaymentType
	public function showPackageType($id)
	{
		$rs  = Package::model()->findByPk($id);
		if($rs->package_type==0){
			$rs->package_type = '一般套餐';
		}elseif($rs->package_type==1){
			$rs->package_type='充值套餐';
		}
		return $rs->package_type;
	}
	//取出所有3G下面服务的套餐
	public function getPackageName()
	{
		$sql="select * from zy_package where service_id = 12";
		$rs=Yii::app()->db->createCommand($sql)->queryAll();
		return CHtml::ListData($rs,'package_id','package_name');
	}
	public function getRechargePackage(){
		$sql="select * from zy_package where package_type = 1";
		$package=Yii::app()->db->createCommand($sql)->queryAll();
		return CHtml::listData($package,'package_id','package_name');
	}
	public function getRechargePackagePrice(){
		$sql="select * from zy_package where package_type = 1";
		$package=Yii::app()->db->createCommand($sql)->queryAll();
		return CHtml::listData($package,'package_id','package_price');
	}
	public function getSmartrouting(){
		$sql="select * from zy_package a left join zy_service b on a.service_id = b.service_id where b.code='smartrouting'";
		$package=Yii::app()->db->createCommand($sql)->queryAll();
		return CHtml::listData($package,'package_id','package_name');
	}
	public function getUserSmartrouting(){
		$login_list_id=yii::app()->request->getParam('login_list_id');
		$sql="select * from zy_package where package_id IN ( select field1 from zy_login_list where login_list_id=".$login_list_id.")";
		$package=Yii::app()->db->createCommand($sql)->queryAll();
		return CHtml::listData($package,'package_id','package_name');
	}
	
	/**
	 * 获取所有电视频道
	 */
	public function getIptvChannels($id){
		if($id != null || $id != ''){
			$sql ="select channel_name from zy_tvchannel where channel_id= ".$id." order by channel_id asc";
		}else{
			$sql="select * from zy_tvchannel order by channel_id asc";
		}
		$result = yii::app()->db->createCommand($sql)->queryAll();
		if($id != null || $id != ''){
			return $result[0]['channel_name'];
		}else{
			return $result;
		}
	}
	
	/**
	 * 获取所有电视频道
	 */
	public function getIptvChannelName($id){
		$sql ="select channel_name from zy_tvchannel where channel_id= ".$id." order by channel_id asc";
		$result = yii::app()->db->createCommand($sql)->queryAll();
		return $result[0];
	}
	
	
	/**
	 * 添加套餐时选择频道
	 */
	public function setIptvChannels($package_id,$channel_id,$tv_name){
		$crate_time = date('Y-m-d h:i:s');
		$crate_user = yii::app()->user->name;
		$sql="INSERT INTO `zy_iptv_package_details` (`package_id`,`channel_id`,`tv_name`,`crate_time`,`crate_user`) VALUES ($package_id,$channel_id,'$tv_name','$crate_time','$crate_user')";
		$result = yii::app()->db->createCommand($sql)->execute();
		if($result > 0){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 更新套餐
	 */
	public function updateIptvChannels($package_id,$channel_id,$tv_name){
		$update_time = date('Y-m-d h:i:s');
		$update_user = yii::app()->user->name;
		$sqlQuery = "select * from zy_iptv_package_details where package_id=".$package_id;
		$results = yii::app()->db->createCommand($sqlQuery)->queryAll();
		//print_r($channel_id);print_r($results);die();
		for($j=0;$j<count($channel_id);$j++){
			$channel_name = $tv_name[$j]['channel_name'];
			if(count($channel_id) > count($results)){
				if(empty($results[$j]['channel_id'])){
					Package::model()->setIptvChannels($package_id,$channel_id[$j],$channel_name);
				}
			}
		}
		for($i = 0; $i < count($results); $i++){
			$channel_name = $tv_name[$i]['channel_name'];
			if($channel_id[$i] == ""){
				$sql="delete from zy_iptv_package_details WHERE `package_details_id` =".$results[$i]['package_details_id'];
			}elseif($results[$i]['package_details_id'] != ""){
				$sql="UPDATE `zy_iptv_package_details` SET `package_id` = '$package_id',`channel_id` = '$channel_id[$i]',`tv_name` ='$channel_name' ,
  				 `update_time` = '$update_time',`update_user` = '$update_user' WHERE `package_details_id` =".$results[$i]['package_details_id'];
			}
			$result = yii::app()->db->createCommand($sql)->execute();
		}
		if($result > 0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 获取SERviceID
	 */
	public function getServiceId($package_id){
		$sql="select service_id from zy_package where package_id=".$package_id;
		$result = yii::app()->db->createCommand($sql)->queryAll();
		return $result[0]['service_id'];
	}
	
	/**
	 * 获取频道名当做套餐内容
	 */
	public function replacePackageContent($package){
		if($package=="")
			return null;
		$channel_id = explode("|",$package);
		for($i=0;$i<count($channel_id);$i++){
			$sql="select * from zy_tvchannel where channel_id=".$channel_id[$i];
			$result[]= yii::app()->db->createCommand($sql)->queryAll();
			$channel_name.= $result[$i][0]['channel_name'].",";
		}
		$channel_name = substr($channel_name,0,strlen($channel_name)-1);
		return $channel_name;
	}
	
}